package pro.cherish.knowledgedemothymeleaf.controller;

import org.springframework.web.bind.annotation.RestController;

import java.io.*;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-02-17 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：Pdf2htmlController
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-02-17 cherish
 */
// @RestController
public class Pdf2htmlController {
    public static void main(String[] args) throws InterruptedException {
        String pdfPath="E:\\Users\\cherishh\\demo.pdf";
        String s = pdf2html(pdfPath);
        System.out.println(s);
    }
    public static final String pdf2htmlexePath = "D:\\迅雷下载\\pdf2htmlEX-win32-0.14.6-upx-with-poppler-data\\pdf2htmlEX.exe";

    public static String pdf2html(String pdfFilePath) throws InterruptedException {
        if (!pdfFilePath.equals("") || pdfFilePath != null) {
            String filePath = pdfFilePath.substring(0, pdfFilePath.lastIndexOf("\\"));
            File htmlFile = new File(filePath + ".html");//html文件路径(包括有html文件名)
            String htmlFilePath = filePath;//html文件路径
            System.out.println("html文件的路径是 " + htmlFilePath);
            String pdfName = pdfFilePath.substring(pdfFilePath.lastIndexOf("\\") + 1);
            String htmlFileName = pdfName.substring(0, pdfName.lastIndexOf(".")) + ".html";//html文件的name
            //pdf2html的插件的路径
            // String pdf2htmlexePath = "D:\\迅雷下载\\pdf2htmlEX-win32-0.14.6-upx-with-poppler-data\\pdf2htmlEX.exe";
            if (!(pdf2htmlexePath != null && !"".equals(pdf2htmlexePath) && pdfFilePath != null
                    && !"".equals(pdfFilePath) && htmlFileName != null && !""
                    .equals(htmlFileName))) {
                System.out.println("传递的参数有误！");
                return null;
            }
            Runtime rt = Runtime.getRuntime();
            StringBuilder command = new StringBuilder();
            command.append(pdf2htmlexePath).append(" ");
            if (htmlFilePath != null && !"".equals(htmlFilePath.trim()))// 生成文件存放位置,需要替换文件路径中的空格
                command.append("--dest-dir ").append(htmlFilePath.replace(" ", "\" \"")).append(" ");
            command.append("--optimize-text 1 ");// 尽量减少用于文本的HTML元素的数目 (default: 0)
            command.append("--zoom 1.4 ");
            command.append("--process-outline 0 ");// html中显示链接：0——false，1——true
            command.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf)
            // ttf,otf,woff,svg
            command.append(pdfFilePath.replace(" ", "\" \"")).append(" ");// 需要替换文件路径中的空格
            if (htmlFileName != null && !"".equals(htmlFileName.trim())) {
                command.append(htmlFileName);
                if (htmlFileName.indexOf(".html") == -1)
                    command.append(".html");
            }
            try {
                Process p = rt.exec(command.toString());
                StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
                // 开启屏幕标准错误流
                errorGobbler.start();
                StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
                // 开启屏幕标准输出流
                long o1 = System.currentTimeMillis();
                outGobbler.start();
                int w = p.waitFor();
                int v = p.exitValue();
                if (w == 0 && v == 0) {
                    System.out.println("转化html文件路径是 " + htmlFilePath);
                    return htmlFilePath + "\\" + htmlFileName;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("要转化的pdf文件不存在");
        }
        return null;
    }

    static class StreamGobbler extends Thread {
        InputStream is;
        String type;

        StreamGobbler(InputStream is, String type) {
            this.is = is;
            this.type = type;
        }

        @Override
        public void run() {
            try {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.out.println(type + ">" + line);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    public void change() {

    }
}
